Ext.namespace("MyPlan");

MyPlan.Modify = function(p) {
	this.winMain = p;
	this.winItem=null;
	this.temp=0;
	this.planId = new Ext.form.Hidden({name:'planId'});
	this.txtState=new Ext.form.Hidden({name:'planState'});
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.planName = new Ext.form.TextField({
		fieldLabel: '计划名称',
		name:'planName',
		width: 300
	});
	this.typeStore  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: [[1, '周计划'], [2, '月计划'], [5, '年度计划']]
    });
	this.planType = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '计划类型',
		name:'planType',
		store: this.typeStore,
		value:"1",	
        valueField: 'value',
        displayField: 'name',
        editable: false,
        allowBlank: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.auditorStore=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'auditors',
	    url: '../myplan/selectAuditors.form?timesnap=' + Date.parse(new Date()),
	    fields: ['userId', 'userName' ]
	});
	this.auditorStore.load();
	this.auditorId = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '审核人',
		name:'auditorId',
		store:this.auditorStore,
        valueField: 'userId',
        displayField: 'userName',
        editable: false,
        allowBlank: false,
        lazyInit:false,
        mode: 'remote',
        triggerAction: 'all',
        selectOnFocus: true
	});
    this.startDate = new Ext.form.DateField({
    	fieldLabel: '计划时间',
    	name:'startDate',
    	allowBlank: false,
		format: 'Y-m-d'
	});
    this.endDate = new Ext.form.DateField({
    	fieldLabel: '～',
    	name:'endDate',
    	allowBlank: false,
    	labelWidth: 20,
    	labelAlign: 'center',
    	labelSeparator: '',
		format: 'Y-m-d'
	});	
	this.form = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        width: 400,
        border: false,
        labelWidth: 70,
        labelAlign: 'right',
        items: [this.planId,this.planName,this.txtState,this.txtRemind,
	        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .5, items: [this.planType]},
		        	            { columnWidth: .5, items: [this.auditorId] } ]
		    },
	        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .45, items: [this.startDate]},
	        	            { columnWidth: .55, items: [this.endDate] } ]
	    }]
    });
	this.fileForm = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        width: 400,
        border: false,
        fileUpload: true, 
        labelWidth: 70,
        labelAlign: 'right',
        items: []
	});
	
	MyPlan.Modify.superclass.constructor.call(this, {
		title: '修改计划',
		autoHeight: true,
        width: 420,
        y: 50,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form,this.fileForm],
		buttons:[{
			text: '下一步',
			cls: 'x-btn-text-icon btn-next',
			handler: this.onNext,
			scope: this
        },/*{
			text: '添加附件',
			iconCls: 'icon-new',
			handler: this.addFile,
			scope: this
        },*/ {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
};
Ext.extend(MyPlan.Modify, Ext.Window,{
	initWin:function(data){
		//alert(Ext.util.JSON.encode(data));
		this.form.getForm().setValues(data);
		this.auditorId.setRawValue(data['auditorName']);
		/*this.fileForm.removeAll(true);
		var conn = new Ext.data.Connection({
			url:'../myplan/searchUpLoadFiles.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		})
		Utils.request(conn,{
			params:{planId:data.planId},
			scope:this,
			success:function(data){
				var files=data.result;
				for(var i=0;i<files.length;i++){
					this.scope.addFile(files[i]);
				}
			}
		});	*/	
	},
	addFile:function(o){
		this.temp+=1;
		var fname="fileName"+this.temp;
		var hname="hideFileId"+this.temp;
		var fileText=(o.ACCESSORY_NAME==null?"请选择要上传附件":o.ACCESSORY_NAME);
		var fileField=new Ext.form.FileUploadField({
	    	fieldLabel: '上传附件',
			emptyText: fileText,
			width : 220,
			name : fname,
			buttonText: '',
			buttonCfg: {
	            iconCls: 'icon-compress'
	        }
	    });
	    var fileDel=new Ext.Button({
	    	text: '清除',
	    	iconCls: 'icon-remove',
	    	scope: this,
	    	handler: this.delFile
	    });
	    if(o.ACCESSORY_ID){
		    var hdFile=new Ext.form.Hidden({
		    	name:hname,
		    	value:o.ACCESSORY_ID
		    });
		    var href='../myplan/downloadFile.form?fileId='+o.ACCESSORY_ID+'&timesnap=' + Date.parse(new Date());
			var lbLink=new Ext.form.Label({
				html:'<a href="'+href+'">'+o.ACCESSORY_NAME+'</>',
				width:280
			});			
			var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] },
		        	         { columnWidth: 1, items: [lbLink] },
		        	         hdFile]
		    };
	    }else{
	    	var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] }
		        	       ]
		    };
	    }   	
		this.fileForm.add(item);
	    this.fileForm.doLayout();
	},
	delFile:function(btn,event){
		var item=btn.ownerCt.ownerCt;
		this.fileForm.remove(item,true);
	    this.fileForm.doLayout();
	},
	onNext: function(btn, event) {
		if (!this.form.getForm().isValid()) return;
		if (this.winItem == null) {
			this.winItem = new MyPlan.Weekly(this);
		}
		if (this.startDate.getValue()>=this.endDate.getValue()){
			Utils.warn2("计划开始时间应小于结束时间！",240);
			return;
		}		
		//判断本周期内有无计划
		var conn = new Ext.data.Connection({
			url:'../myplan/checkPlan.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		});
		var param=this.form.getForm().getValues();
		Utils.request(conn,{
			params:param,
			scope:this,
			success:function(data){
				if(data.result>0){
					Utils.warn2("该时间段内已存在其他计划，请修改计划时间！",240);					
				}else{
					this.scope.winItem.initWin(this.scope.form.getForm().getFieldValues());
			        this.scope.winItem.show();
					this.scope.hide();
				}
			}
		});		
	}
});
